﻿using UnityEngine;

namespace UFrame
{
    public static class GameObjectTool
    {
        /// <summary>
        /// if GameObject active. not do active.
        /// </summary>
        /// <param name="go"></param>
        /// <param name="display"></param>
        /// <param name="activeSelf"></param>
        public static void TryActive(this GameObject go, bool display, bool activeSelf = true)
        {
            if (activeSelf)
            {
                if (go.activeSelf != display)
                {
                    go.SetActive(display);
                }
            }
            else
            {
                if (go.activeInHierarchy != display)
                {
                    go.SetActive(display);
                }
            }
        }

        public static void SetAllLayer(this GameObject go, int layer)
        {
            if (null == go)
            {
                return;
            }

            Transform[] children = go.GetComponentsInChildren<Transform>();
            for (int i = 0; i < children.Length; ++i)
            {
                children[i].gameObject.layer = layer;
            }

            go.layer = layer;
        }

        public static void SetActive(GameObject go, bool display)
        {
            if (go.activeSelf != display)
            {
                go.SetActive(display);
            }
        }
    }
}